package com.zhuhjay.ruiji.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MybatisPlus 自动字段填充
 * @author ZhuHJay
 * @date 2022/5/16 13:16
 */
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入数据时填充 JavaBean 对象
     * @param metaObject 元数据对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrent());
        metaObject.setValue("updateUser", BaseContext.getCurrent());
    }

    /**
     * 更新数据时填充
     * @param metaObject 元数据对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrent());
    }
}
